package com.hwadee.hello.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.hwadee.hello.entity.Classlist;
import com.hwadee.hello.entity.Student;
import com.hwadee.hello.service.StudentService;

@Controller
//@RequestMapping("/u")
public class StudentsController {
	@Autowired
	private StudentService studentService;
	
	//@RequestMapping(value = "/students", method = RequestMethod.GET)
	
	@GetMapping("/students") 
	public String get(@RequestParam(value = "p",defaultValue = "1")int p, Model model) { 
		PageInfo<Student> pi =studentService.getStudents(p); 
		model.addAttribute("pi", pi); 
		return "student/list"; 
	}
	 
	/*
	 * @GetMapping("/students") public ResponseEntity<PageInfo<Student>>
	 * get(@RequestParam(value = "p", defaultValue = "1") int p, String wd) {
	 * PageInfo<Student> pi = studentService.getStudents(p);
	 * //model.addAttribute("pi", pi); //WEB-INF/views/student/list.jsp return new
	 * ResponseEntity<PageInfo<Student>>(pi, HttpStatus.OK); }
	 */
	
	@GetMapping("/delete/{id}")
	public String delete(@PathVariable("id")int id) {
		int rows = studentService.removeStudent(id);
		return "redirect:/students";
	}
	
	@GetMapping("/add")
	public String add() {
		return "student/add";
	}
	
	@PostMapping("/add")
	public String added(Student stu) {
		
		studentService.add(stu);
		return "redirect:/students";
	}
	
	
	@GetMapping("/update/{id}")
	public String update(@PathVariable("id")int id, Model model) {
		Student stu = studentService.findById(id);
		model.addAttribute("stu", stu);
		return "student/update";
	}
	
	@PostMapping("/update")
	public String updated(Student stu) {

		studentService.update(stu);
		return "redirect:/students";
	}

	@GetMapping("/date")
	public @ResponseBody Date date(@DateTimeFormat(pattern = "yyyy-MM-dd")Date birth) {
		
		return birth;
	}
	
	@PostMapping("/array")
	public @ResponseBody String array(String [] hobbies) {
		
		return Arrays.toString(hobbies);
	}


	// list: student[0].stuName
	@PostMapping("/list")
	public @ResponseBody List<Student> list(Classlist list) {
		
		return list.getStudents();
	}
	
	/*
	 * { "stuId": null, "stuName": "zhangsan", "stuSex": "M", "stuAge": 0, "stuPro":
	 * null, "stuGrade": null, "stuIntegrity": 0 }
	 */
	@PostMapping("/json")
	public @ResponseBody Student json(@RequestBody Student stu) {
		return stu;
	}
	
}
